//
//  SWShareManager.swift
//  MiU
//
//  Created by linhan on 15/12/22.
//  Copyright © 2015年 MiU. All rights reserved.
//

import Foundation
import UIKit

private let _manager = SWShareManager()

enum SWShareChannel:Int
{
    case QQ
    case Weibo
    case WechatSession
    case WechatSpace
}

extension WechatShareScene
{
    init(channel: SWShareChannel)
    {
        if channel == .WechatSession
        {
            self = .Session
        }
        else
        {
            self = .Space
        }
    }
}

class SWShareManager: NSObject
{
    static let ShareChannelKey:String = "Channel"
    
    
    class func sharedManager() -> SWShareManager
    {
        return _manager
    }
    
    override init()
    {
        super.init()
        
    }
    
    func share(image:UIImage, channel:SWShareChannel)
    {
        if let data:Data = UIImageJPEGRepresentation(image, 0.8)
        {
            if channel == .WechatSession || channel == .WechatSpace
            {
                //微信 缩略图控制在80*80以内，防止超过30K
                let thumb:UIImage = ImageUtil.getImageWithinByteLimit(image, size:CGSizeMake(80, 80), limit: 25 * 1024)
                let scene = WechatShareScene(channel: channel)
                WeixinHelper.shared().sendImage(thumb, imageData: data, scene: scene.rawValue)
            }
            else if channel == .QQ
            {
                QQSDKHelper.shared().shareImage(data, previewImageData: data, scene: .Session)
            }
            else
            {
                WeiboHelper.shared().publish(text: "#咿呀#", imageData: data)
            }
        }
    }
    
    func share(url:String, channel:SWShareChannel, title:String? = nil, desc:String? = nil)
    {
        if channel == .WechatSession || channel == .WechatSpace
        {
            let scene = WechatShareScene(channel: channel)
            WeixinHelper.shared().sendLink(url, scene: scene.rawValue, title: title, desc: desc)
        }
        else if channel == .QQ
        {
            if let url = URL(string: url)
            {
                QQSDKHelper.shared().shareLink(url, scene: QQSDK.ShareScene.Space, title: title ?? "", description: desc ?? "", previewImageURL: nil)
            }
        }
        else
        {
            WeiboHelper.shared().publish(text: url, imageData: nil)
        }
    }
    
    
    
    
}



